package com.frame.shiro;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.mini.core.utils.StrKit;

public class OnLineFilter extends FormAuthenticationFilter {

    private static final Log logger = LogFactory.getLog(OnLineFilter.class);

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
    	if (isLoginRequest(request, response)) {
            if (isLoginSubmission(request, response)) {
                return executeLogin(request, response);
            } else {
                return true;
            }
        } else {
            saveRequest(request);
            String loginUrl = getLoginUrl();
            String reqUrl = ((HttpServletRequest) request).getRequestURI();
            loginUrl = StrKit.notBlank(reqUrl)&&!"/".equals(reqUrl)&&!"/index".equals(reqUrl)&&!"/index/".equals(reqUrl)? loginUrl+"#"+reqUrl:loginUrl;
            WebUtils.issueRedirect(request, response, loginUrl);
            return false;
        }
    }

}
